package ws.servlet;

import java.rmi.RemoteException;
import java.util.*;

import org.json.*;

import ws.search.SearchWSProxy;
import cs236369.hw5.SearchWS;

class KeywordsSearcher implements WSSearcher {
	private final String[]	keywords;
	
	private KeywordsSearcher(JSONArray array) {
		Collection<String> $ = new ArrayList<>(array.length());
		for (int i = 0; i < array.length(); i++) {
			$.add(array.getString(i));
		}
		
		keywords = new String[$.size()];
		$.toArray(keywords);
	}
	
	public KeywordsSearcher() {
		// do nothing
		keywords = new String[0];
	}
	
	@Override
	public WSSearcher build(JSONObject parameters) throws JSONException {
		JSONArray array = parameters.getJSONArray("array");
		return new KeywordsSearcher(array);
	}
	
	@Override
	public String[] search(String endpoint) throws RemoteException {
		SearchWS endpointService = new SearchWSProxy(endpoint);
		String[] serviceResults = endpointService.searchKeywords(keywords);
		return serviceResults;
	}
	
}
